<?php
##############################################
#
#   管理员列表
# @ Time : 2017-1-1 - 下午7:45:34
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Admin\Controller;
use Admin\Model\AdminBaseModel;
use Admin\Model\ApplyModel;

class ApplyController extends AdminBaseController{
	
    protected $_apply;
	
	public function _initialize(){
		parent::_initialize();
		$this->_apply = new ApplyModel();
	}
    
	/**
	 * 申情列表
	 */
    public function applyList(){
        if(IS_GET){
            $user_name = I('get.user_name');
            $phone = I('get.phone');
            $status = I('get.status');
            $type = I('get.type');
            if($user_name){
                $data['user_name'] = array('like','%'.$user_name.'%');
            }
            if($phone){
                $data['phone'] = array('like','%'.$phone.'%');
            }
            if($status){
                $data['status'] = array('eq',$status);
            }
            if($type){
                $data['type'] = array('eq',$type);
            }
        }
        $value = M('apply');
        $count =$value->where($data)->count();
        $Page = new \Think\Page($count,10);
        $show = $Page -> show();
        $result = $value->where($data)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('apply',$result);
        $this->assign('page',$show);
        $this -> display();
    }
	
    
     /**
     *状态展示
     * @Date 201707
     * @Author Lin
     */
    public function statusEdit()
    {
        $Id = I('Id');
        $status = I('status');
        $model = I('model');
        //var_dump($menuId);var_dump($status);die;

        $result = M($model)->where(array('id'=>$Id))->setField('status',$status);

        if ($result !== false)
            $this->ajaxReturn(array('status'=>1));
        else
            $this->ajaxReturn(array('status'=>0));
    }
    
    
        /**
     * 单条删除
     */
    public function dataDel()
    {
        if(!IS_AJAX) return;
        $model = I('model');
//        var_dump($model);die;
        $result = M($model)-> delete(intval($_GET['id']));
        if($result)
            $this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
        else
            $this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
    }
    /**
     *全选删除
     * @Date 201703
     * @Author Linsenquan
     */
    public function deleteAll()
    {
        //var_dump($_POST);die;
        if(!IS_POST)
        $this->error('非法操作');
        if(empty($_POST))
            $this->error('请选择要删除的信息！','',1);
       //var_dump($_POST);die;
        $result = $this->_serveObject->deleteAllData($_POST);
        if($result)
            $this->success('删除成功','',1);
        else
            $this->error('删除失败','',1);
    }
    
    public function applyView(){
        if(IS_GET){
            $id = I('id');
            $act = I('act');
            if($id && $act == 'edit'){
                $value = M('apply')->where('id='.$id)->find();
                $more_img = unserialize($value['more_img']);
                $value['more_img_list'] = $more_img['M'];
                $this->assign('data',$value);
            }
            $category = M('apply_category')->order('sort asc')->select();
            $this->assign('act', $act);
            $this->assign('category', $category);
            $this->display(); 
        }elseif(IS_POST){
            $data = I('post.');
//            var_dump($data);die;
            //一寸证件照
            if ($_FILES['headimgurl']['name']) {
                $photo_img = AdminBaseModel::upload(900, 255);
                $data['headimgurl'] = $photo_img['headimgurl'];  //serialize($photo_img)
                if ($data['old_headimgurl']) {
                    @unlink($_SERVER['DOCUMENT_ROOT'] . substr(__APP__, 1) . $data['old_headimgurl']);
                }
            }
//            else {
//                if (!$data['old_headimgurl']) {
//                    $this->error('请上传一寸证件照', '', 1);
//                }
//            }
            //证明材料图片
            if ($_FILES['img']['name']) {
                $photo_img = AdminBaseModel::upload(900, 255);
                $data['img'] = $photo_img['img'];  //serialize($photo_img)
                if ($data['old_img']) {
                    @unlink($_SERVER['DOCUMENT_ROOT'] . substr(__APP__, 1) . $data['old_img']);
                }
            } else {
                if (!$data['old_img']) {
                    $this->error('请上传材料证明图片', '', 2);
                }
            }
            
            //照片墙
            if ($_FILES['more_img']['name'][0]) {
                if(count($_FILES['more_img']['name'])>10){
                    $this->error('最多只能上传十张照片',1);
                }
                $photo_img = AdminBaseModel::uploadImg();
                $data['more_img'] = serialize($photo_img);  //serialize($photo_img)
                if ($data['old_more_img']) {
                    @unlink($_SERVER['DOCUMENT_ROOT'] . substr(__APP__, 1) . $data['old_more_img']);
                }
            }
//            var_dump($data);die;
            if($data['act']=='add'){
                $data['add_time'] = time();
                $apply = M('apply')->add($data);
                 //发送给用户消息
		    self::sendMessage($data['id'], $data['user_id'], $data['status'], '专家申请', $data['reason']);
                if($apply){
                    $this->success('添加成功',U("Apply/applyList"),1);
                }else{
                    $this->error('添加失败','',1);
                }
            }elseif ($data['act']=='edit') {
                $apply = M('apply')->where('id='.$data['id'])->save($data);
                 //发送给用户消息
		    self::sendMessage($data['id'], $data['user_id'], $data['status'], '专家申请', $data['reason']);
                if($apply){
                    $this->success('修改成功',U("Apply/applyList"),1);
                }else{
                    $this->error('修改失败','',1);
                }
            }
            
        }
        
    }
    
    /**
	 * 操作
	 */
	public function operate(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.id');
	    if(!in_array($operate,array('del','pass','wait','defeated')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
            $user_ids = M('apply')->where($map)->field('user_id,id')->select();
	    $users = M('apply');
	    switch ($operate){
	        case 'del':
	            $row = $users->where($map)->delete();
	            break;
	        case 'pass':
	            $data = array('status'=>1);
                    //发送给用户消息
                    foreach ($user_ids as $k=>$v){
                        self::sendMessage($v['id'], $v['user_id'], 1, '专家申请', '');
                    }
	            $row = $users->where($map)->save($data);
	            break;
	        case 'wait':
	            $data = array('status'=>2);
	            $row = $users->where($map)->save($data);
	            break;
                case 'defeated':
	            $data = array('status'=>3);
                    //发送给用户消息
                    foreach ($user_ids as $k=>$v){
                        self::sendMessage($v['id'], $v['user_id'], 3, '专家申请', '');
                    }
	            $row = $users->where($map)->save($data);
	            break;
	    }
	    if(!$row)
	        $this->error('操作失败');
	    else
	        $this->success('操作成功', U('applyList'), 1);
	
	}
        
        /**
	 * 发送专家消息
	 */
	public function sendMessage($apply_id, $user_id, $status, $name, $reason){
	    
	    switch ($status) {
	        case 1:
	        $data = array(
                    'apply_id'=>$apply_id,
                    'user_id'=>$user_id,
	            'title'=>'专家申请审核通过', 
	            'content'=>'恭喜你，你的 "'.$name.'" 已经审核通过',
	            'auth'=>'admin',
                    'add_time'=>time());
	        M('apply_message')->add($data);
	        break;
	        case 3:
                    $data = array(
                    'apply_id' => $apply_id,
                    'user_id' => $user_id,
                    'title' => '专家申请审核不予通过',
                    'content' => '抱歉，你的  "' . $name . '" 审核不予通过。<br/><p style="color:red;">原因：' . $reason . '</p>',
                    'auth' => 'admin',
                    'add_time' => time());
                M('apply_message')->add($data);;
	        break;
	    }
	    return true;
	}
        
         /**
	 * 专家类型
	 */
        public function category_list(){
            $data =  $this->_apply->getApplyCategory(I('name'));
            $this -> assign('category', $data[0]);
	    $this -> assign('page', $data[1]);
            $this->display();
        }
        
         /**
	 * 专家类型添加
	 */
        public function add_category() {
        if (IS_GET) {
            $id = I('id');
            $act = I('get.act');
            if ($id && $act == 'edit') {
                $data = $this->_apply->getCategoryFind($id);
                $this->assign('id', $id);
                $this->assign('category', $data);
            }
            $this->assign('act', $act);
            $this->display();
        } else if (IS_POST) {
            $data = array(
                'name' => I('name'),
                'sort' => I('sort'),
                'is_show' => I('is_show'),
                'add_time' => time()
            );
            $apply_base_model = new AdminBaseModel('apply_category');
            $act = I('post.act');
            if ($act == 'add') {
                $result = $apply_base_model->addData($data);
                if ($result)
                    $this->success('添加成功', U('Apply/category_list'), 1);
                else
                    $this->error('操作失败', '', 1);
            }elseif ($act == 'edit') {
                $id = I('id');
                if ($this->_apply->editApplyCategory($id, $data) !== false)
                    $this->success('修改成功', U('Apply/category_list'), 1);
                else
                    $this->error('操作失败', '', 1);
            }
        }
    }
    
    /**
	 * 删除类型
	 */
	public function deleteCategory(){
            if(!IS_AJAX) return;
		$result = $this->_apply->deleteCategory(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
                    $this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
                
        }
        
        /**
	 * 操作
	 */
	public function operate_category(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.category_id');
	    if(!in_array($operate,array('show','hide','del')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $category = M('apply_category');
	    switch ($operate){
	        case 'del':
	            $row = $category->where($map)->delete();
	            break;
	        case 'show':
	            $data = array('is_show'=>1);
	            $row = $category->where($map)->save($data);
	            break;
	        case 'hide':
	            $data = array('is_show'=>2);
	            $row = $category->where($map)->save($data);
	    }
	    if($row !== false)
	        $this->success('操作成功', U('category_list'), 1);
	    else
	        $this->error('操作失败');
	}
        
        
}